package com.example.firsttest;

import ch.qos.logback.core.util.FileUtil;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Set;

public class AutoTest02 {
    public void test01() throws InterruptedException, IOException {
        System.setProperty("webdriver.edge.driver", "D:\\driver\\msedgedriver.exe");
        EdgeOptions options = new EdgeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver driver = new EdgeDriver(options);
        driver.get("https://www.baidu.com/");

//        driver.findElement(By.cssSelector("#kw")).sendKeys("riize");
//        driver.findElement(By.cssSelector("#su")).click();

//        driver.findElement(By.cssSelector("#kw")).sendKeys("我爱游戏");
//        Thread.sleep(1000);
//        driver.findElement(By.cssSelector("#kw")).sendKeys("我爱学习");
//        Thread.sleep(1000);
//        driver.findElement(By.cssSelector("#su")).click();
//        Thread.sleep(1000);

//        driver.findElement(By.cssSelector("#kw")).sendKeys("我爱游戏");
//        Thread.sleep(1000);
//        driver.findElement(By.cssSelector("#kw")).clear();
//        Thread.sleep(1000);
//        driver.findElement(By.cssSelector("#kw")).sendKeys("我爱学习");
//        Thread.sleep(1000);
//        driver.findElement(By.cssSelector("#su")).click();
//        Thread.sleep(1000);

//        String text = driver.findElement(By.xpath("//*[@id=\"hotsearch-content-wrapper\"]/li[1]/a/span[2]")).getText();
//        System.out.println("打印的内容是：" + text);

//        String title = driver.getTitle();
//        System.out.println("当前页面标题为：" + title);
//
//        String currentUrl = driver.getCurrentUrl();
//        System.out.println("当前页面 url : " + currentUrl);

//        driver.findElement(By.xpath("//*[@id=\"s-top-left\"]/a[6]")).click();
//        Thread.sleep(1000);
//        String curWindow = driver.getWindowHandle();
//        Set<String> allWindows = driver.getWindowHandles();
//        for(String w : allWindows){
//            if(w != curWindow){
//                driver.switchTo().window(w);
//            }
//        }
//        driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[1]/div/div[3]/div[3]/span")).getText();
//        Thread.sleep(10000);


//        driver.findElement(By.xpath("//*[@id=\"s-top-left\"]/a[6]")).click();
////        driver.manage().window().minimize();
//        driver.manage().window().maximize();
//        Thread.sleep(5000);

//        driver.findElement(By.xpath("//*[@id=\"s-top-left\"]/a[6]")).click();
//        File file = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//        FileUtils.copyFile(file, new File("my.png"));


//        SimpleDateFormat sim1 = new SimpleDateFormat("yyyy-MM-dd");
//        SimpleDateFormat sim2 = new SimpleDateFormat("HHmmssSS");
//        String dirTime = sim1.format(System.currentTimeMillis());
//        String fileTime = sim2.format(System.currentTimeMillis());
//        String filename = "./src/test/image/" + dirTime + "/test_" + fileTime + ".png";
//
////        // 1. 先获取文件所在的目录（日期文件夹）
////        File destFile = new File(filename);
////        File parentDir = destFile.getParentFile(); // 对应 "./src/test/image/2025-10-05"
////
////        // 2. 自动创建目录（如果不存在，包括多级目录）
////        if (!parentDir.exists()) {
////            parentDir.mkdirs(); // 关键：创建所有不存在的父目录
////        }
//
//        File imageFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//        FileUtils.copyFile(imageFile, new File(filename));
////        FileUtils.copyFile(imageFile, destFile);
//        System.out.println("截图保存成功：" + filename);
//

        driver.findElement(By.xpath("//*[@id=\"s-top-left\"]/a[6]")).click();
        Thread.sleep(3000);
        String curWindow = driver.getWindowHandle();
        Set<String> allWindows = driver.getWindowHandles();
        for(String w : allWindows){
            if(w != curWindow){
                driver.switchTo().window(w);
            }
        }
        driver.close();
        driver.switchTo().window(curWindow);
        driver.findElement(By.xpath("//*[@id=\"s-top-left\"]/a[1]")).click();
        Thread.sleep(3000);
        driver.quit();
    }

}
